﻿using FreesenseSubAIS.ImageProcess;
using FreesenseSubAIS.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FreesenseSubAIS.UI
{
    public partial class CameraSet : Form
    {
        public CameraSet()
        {
            InitializeComponent();
        }
        static CameraSet setform = new CameraSet();
        public static CameraSet Entrance()
        {
            if (setform == null || setform.IsDisposed)
            {
                setform = new CameraSet();
            }
            return setform;
        }
        private void SaveCameraInfo_Btn_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView1.Rows.Count > 0)
                {
                    var clist = new List<CameraInfomation>();
                    for (int i = 0; i < radGridView1.Rows.Count; i++)
                    {
                        CameraInfomation cs = new CameraInfomation();
                        cs.CameraName = radGridView1.Rows[i].Cells[0].Value.ToString();
                        cs.MacAddr = radGridView1.Rows[i].Cells[1].Value.ToString();
                        cs.CameraIp = radGridView1.Rows[i].Cells[2].Value.ToString();
                        cs.SubnetMask = radGridView1.Rows[i].Cells[3].Value.ToString();
                        cs.DefaultGateway = radGridView1.Rows[i].Cells[4].Value.ToString();
                        cs.LocalIP = radGridView1.Rows[i].Cells[5].Value.ToString();
                        cs.LocalAdapterName = radGridView1.Rows[i].Cells[6].Value.ToString();
                        cs.CameraType = int.Parse(radGridView1.Rows[i].Cells[7].Value.ToString());
                        cs.Enable = bool.Parse(radGridView1.Rows[i].Cells[8].Value.ToString());
                        clist.Add(cs);
                    }
                    if (radGridView2.Rows.Count > 0)
                    {
                        CameraInfomation cs2 = new CameraInfomation();
                        cs2.CameraName = radGridView2.Rows[0].Cells[0].Value.ToString();//从机名称
                        cs2.MacAddr = radGridView2.Rows[0].Cells[1].Value.ToString();//从机端口号
                        cs2.CameraIp = radGridView2.Rows[0].Cells[2].Value.ToString();//主机IP
                        cs2.SubnetMask = radGridView2.Rows[0].Cells[3].Value.ToString();//从机服务端口
                        cs2.DefaultGateway = radGridView2.Rows[0].Cells[4].Value.ToString();//默认网关
                        cs2.LocalIP = radGridView2.Rows[0].Cells[5].Value.ToString();//本地IP
                        cs2.LocalAdapterName = radGridView2.Rows[0].Cells[6].Value.ToString();//本地网卡名称
                        Device.Parameter.SubNetInfo = cs2;
                    }
                    if (Device.Parameter.CameraList != null)
                        Device.Parameter.CameraList.Clear();
                    Device.Parameter.CameraList = clist;
                    Device.SaveParameter();
                    MessageBox.Show("相机及从机IP保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void CameraSet_Load(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < Device.Parameter.CameraList.Count; i++)
                {
                    var cI = Device.Parameter.CameraList[i];
                    if (cI != null)
                        radGridView1.Rows.Add(new object[] { cI.CameraName, cI.MacAddr, cI.CameraIp, cI.SubnetMask, cI.DefaultGateway, cI.LocalIP, cI.LocalAdapterName, cI.CameraType, cI.Enable });
                }
                CameraInfomation cI2 = new CameraInfomation();
                cI2 = Device.Parameter.SubNetInfo;
                if (cI2 != null)
                    radGridView2.Rows.Add(new object[] { cI2.CameraName, cI2.MacAddr, cI2.CameraIp, cI2.SubnetMask, cI2.DefaultGateway, cI2.LocalIP, cI2.LocalAdapterName });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void CameraSet_FormClosing(object sender, FormClosingEventArgs e)
        {
            radGridView1.Rows.Clear();
            setform.Dispose();
        }

        private void UpMove_Btn_Click(object sender, EventArgs e)
        {
            if (radGridView1.Rows.Count > 0)
            {
                if (radGridView1.SelectedRows.Count > 0)
                {
                    int selrowIndex = radGridView1.SelectedRows[0].Index;
                    if (selrowIndex > 0)//从1开始
                    {
                        string[] strtemp = { "", "", "", "", "", "", "" };
                        for (int i = 0; i < radGridView1.Rows[selrowIndex].Cells.Count; i++)
                        {
                            strtemp[i] = radGridView1.Rows[selrowIndex].Cells[i].Value.ToString();
                        }
                        for (int j = 0; j < radGridView1.Rows[selrowIndex].Cells.Count; j++)
                        {
                            radGridView1.Rows[selrowIndex].Cells[j].Value = radGridView1.Rows[selrowIndex - 1].Cells[j].Value;
                        }
                        for (int j = 0; j < radGridView1.Rows[selrowIndex].Cells.Count; j++)
                        {
                            radGridView1.Rows[selrowIndex - 1].Cells[j].Value = strtemp[j];//Insert (selrowIndex,)

                        }

                        //Telerik.WinControls.UI.GridViewRowInfo item = new Telerik.WinControls.UI.GridViewRowInfo(radGridView1);
                        //item = radGridView1.Rows[selrowIndex - 1];
                        //radGridView1.Rows.RemoveAt(selrowIndex - 1);
                        //radGridView1.Rows .Insert (selrowIndex,item);
                    }
                }
            }
        }

        private void DownMove_Btn_Click(object sender, EventArgs e)
        {
            if (radGridView1.Rows.Count > 0)
            {
                if (radGridView1.SelectedRows.Count > 0)
                {
                    int selrowIndex = radGridView1.SelectedRows[0].Index;
                    if (selrowIndex < radGridView1.Rows[selrowIndex].Cells.Count)//从1开始
                    {
                        string[] strtemp = { "", "", "", "", "", "", "" };
                        for (int i = 0; i < radGridView1.Rows[selrowIndex].Cells.Count; i++)
                        {
                            strtemp[i] = radGridView1.Rows[selrowIndex].Cells[i].Value.ToString();
                        }
                        for (int j = 0; j < radGridView1.Rows[selrowIndex].Cells.Count; j++)
                        {
                            radGridView1.Rows[selrowIndex].Cells[j].Value = radGridView1.Rows[selrowIndex + 1].Cells[j].Value;
                        }
                        for (int j = 0; j < radGridView1.Rows[selrowIndex].Cells.Count; j++)
                        {
                            radGridView1.Rows[selrowIndex + 1].Cells[j].Value = strtemp[j];//Insert (selrowIndex,)

                        }
                    }
                }
            }
        }
    }
}
